package cn.hxth.cloud.api.common.util;

import cn.hxth.cloud.api.rabbitmq.RabbitMQSender;
import cn.hxth.cloud.api.rabbitmq.RabbitQueue;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.UUID;
public class AbstractBaseServiceImpl {
    protected RabbitMQSender rabbitMQSender ;
    protected RedisTemplate<String,String> redisTemplate ;
    protected DateProvider dateProvider ;



    public AbstractBaseServiceImpl(RabbitMQSender rabbitMQSender, RedisTemplate<String, String> redisTemplate,DateProvider dateProvider) {
        this.rabbitMQSender = rabbitMQSender;
        this.redisTemplate = redisTemplate;
        this.dateProvider = dateProvider;

    }

    protected synchronized   void sendMessage(String name,Object data,String eventType,String mid)throws Exception{
        Long wait = 0l ;
        JSONObject jsonObject  = new JSONObject() ;

        Boolean flag = null ;
        String messageId = UUID.randomUUID().toString() ;
        jsonObject.put("eventType",eventType) ;
        jsonObject.put("data",data) ;
        jsonObject.put("messageId",messageId) ;
        if(mid!=null){
            jsonObject.put("mid",mid) ;
        }
        this.rabbitMQSender.send(RabbitQueue.DATA_CHANGE_QUEUE+"-"+name,jsonObject.toJSONString());
       /* while (true){

            if(this.redisTemplate.opsForValue().get(messageId) == null){
                System.out.println(this.redisTemplate.opsForValue().get(messageId)+"------------------------------------"+messageId);
                Thread.sleep(20);
                wait = wait+20 ;
                continue;
            }
            else{
                flag = Boolean.valueOf(this.redisTemplate.opsForValue().get(messageId)) ;
                break;
            }
        }*/
       /* this.redisTemplate.delete(messageId);*/
        /*if(!flag){
            {
                throw new RuntimeException("相关微服务处理失败") ;
            }

        }*/

    }
}
